package cn.wolfcode.crm.web.controller;


import cn.wolfcode.crm.domain.Product;
import cn.wolfcode.crm.query.ProductQueryObject;
import cn.wolfcode.crm.service.IProductService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.PermissionName;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("product")
public class ProductController {

	@Autowired
	private IProductService productService;

	//打开页面和数据获取分开
	@RequestMapping("")
	@RequiresPermissions("product:index")
	@PermissionName("产品主页")
	public String view() {
		return "product/list";
	}

	@RequestMapping("query")
	@ResponseBody
	@RequiresPermissions("product:query")
	@PermissionName("产品数据")
	public PageResult query(ProductQueryObject qo) {
		PageResult result = productService.query(qo);
		return result;
	}

	@RequestMapping("getProduct")
	@ResponseBody
	public Product getProduct(Long id) {
		Product product = productService.selectByPrimaryKey(id);
		return product;
	}

	@RequestMapping("selectAll")
	@ResponseBody
	public List<Product> selectAll() {
		List<Product> products = productService.selectAll();
		return products;
	}

	@RequestMapping("saveOrUpdate")
	@ResponseBody
	@RequiresPermissions("product:saveOrUpdate")
	@PermissionName("产品保存和更新")
	public JsonResult saveOrUpdate(Product product) {
        JsonResult result = new JsonResult();
		try {
			productService.saveOrUpdate(product);
		} catch (Exception e) {
			e.printStackTrace();
            result = new JsonResult("操作失败");
		}
		return result;
	}

	@RequestMapping("delete")
	@ResponseBody
	@RequiresPermissions("product:delete")
	@PermissionName("产品删除")
	public JsonResult delete(Long id) {
        JsonResult result = new JsonResult();
		try {
			productService.deleteByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
            result = new JsonResult("操作失败");
		}
		return result;
	}
	@RequestMapping("selectProductList")
	@ResponseBody
	public List<Product> selectProductListByBillId(Long number) {
		return productService.selectProductListByBillNumber(number);
	}

	@RequestMapping("queryProductByCompany")
	@ResponseBody
	public List<Product> queryProductByCompany(Long number) {
		return productService.selectProductListByBillNumber(number);
	}


}
